package cn.hdy.org.controller;

import cn.hdy.base.util.AjaxResult;
import cn.hdy.base.util.PageList;
import cn.hdy.org.domain.Employee;
import cn.hdy.org.query.EmployeeQuery;
import cn.hdy.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理的CRUD", description = "员工管理的CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    @ApiOperation(value = "查询所有员工")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.findOne(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个员工")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除员工")
    public AjaxResult delPatch(@RequestBody Long[] ids) {
        employeeService.patchDel(ids);
        return AjaxResult.me();
    }

    @PutMapping
    @ApiOperation(value = "添加或修改员工")
    public AjaxResult addOrUpdate(@RequestBody @Validated Employee employee) {

        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me();
    }

    @PostMapping
    @ApiOperation(value = "员工的分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

}
